﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

namespace smartphone.App_Code
{
    public class Promotion
    {
        /// <summary>
        /// Lay danh sach tat ca khuyen mai trong cua hang
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllPromotion()
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "GetPromotion";

            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            return table;
        }


        /// <summary>
        /// Xoa 1 khuyen mai trong cua hang
        /// </summary>
        /// <param name="maKhuyenMai"></param>
        /// <returns></returns>
        public bool DeletePromotion(int maKhuyenMai)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "DeletePromotion";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maKhuyenMai";
            param.Value = maKhuyenMai;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }


        /// <summary>
        /// Tao khuyen mai
        /// </summary>
        /// <param name="ngayBatDau"></param>
        /// <param name="ngayKetThuc"></param>
        /// <param name="noiDung"></param>
        /// <param name="tinhTrang"></param>
        /// <returns></returns>
        public bool CreatePromotion(DateTime ngayBatDau, DateTime ngayKetThuc, string noiDung, bool giamGia)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "CreatePromotion";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@ngayBatDau";
            param.Value = ngayBatDau;
            param.DbType = DbType.Date;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@ngayKetThuc";
            param.Value = ngayKetThuc;
            param.DbType = DbType.Date;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@noiDung";
            param.Value = noiDung;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@giamGia";
            param.Value = giamGia;
            param.DbType = DbType.Boolean;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }


        /// <summary>
        /// Cap nhat thong tin khuyen mai
        /// </summary>
        /// <param name="ngayKetThuc"></param>
        /// <param name="noiDung"></param>
        /// <param name="tinhTrang"></param>
        /// <returns></returns>
        public bool UpdatePromotion(string maKhuyenMai, DateTime ngayKetThuc, DateTime ngayBatDau, string noiDung, bool giamGia)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "UpdatePromotion";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@ngayKetThuc";
            param.Value = ngayKetThuc;
            param.DbType = DbType.Date;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@maKhuyenMai";
            param.Value = maKhuyenMai;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@ngayBatDau";
            param.Value = ngayBatDau;
            param.DbType = DbType.Date;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@noiDung";
            param.Value = noiDung;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@giamGia";
            param.Value = giamGia;
            param.DbType = DbType.Boolean;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }
    }
}